package com.sleep.ruledemo;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;


@SpringBootTest
@DisplayName("JUnit测试用例")
@Slf4j
public class Junit {

    private Long startTime;
    private Long endTime;

    @Disabled // 用于标记一个测试方法或测试类被禁用 JUnit 将不会执行该测试
    @Test // junit5 需要使用org.junit.jupiter.api下的@Test
    public void testDisadle(){
        log.debug("testDisadle");
    }

    @Timeout(value = 2L,unit = TimeUnit.SECONDS)
    @Test
    public void testTimeout() throws InterruptedException {
        Thread.sleep(1000);
        log.info("testTimeout");
    }

    // 重复测试
    @RepeatedTest(3)
    void testRepate(){
        log.info("testRepate");
    }

    @DisplayName("测试带参数的测试方法")
    @ParameterizedTest
    @ValueSource(strings = {"1","2","3"}) // 数据源的每个参数都会执行一次测试方法
    void testParameterized(String str){
        log.info("{}",str);
    }

    @DisplayName("通过函数获取参数")
    @ParameterizedTest
    @MethodSource(value = "getParams")
    void testMethodParams(String str){
        log.info("函数获取参数：{}",str);
    }

    @DisplayName("测试异步编排")
    @Test
    void testAsync(){
        List<String> list = List.of("1","2","3");
        List<CompletableFuture<String>> list1 = list.stream().map(item -> CompletableFuture.supplyAsync(() -> {
            log.info("异步处理：{}", item);
            return item+ Thread.currentThread().getName();
        })).toList();
        CompletableFuture.allOf(list1.toArray(new CompletableFuture[0])).join();
        list1.forEach(item -> {
            log.warn("异步处理结果：{}",item.join());
        });
    }

    private static List<String> getParams(){
        return List.of("1","2","3");
    }

    @DisplayName("断言测试")
    @Test
    void testAssert(){
        Assertions.assertEquals(1,1);
        Assertions.assertTrue(true);
        Assertions.assertFalse(false);
        Assertions.assertNull(null);
        Assertions.assertNotNull(new Object());
        Assertions.assertEquals("666","666");
    }

    @BeforeEach
    public void setUp() {
        System.out.println("执行开始时间"+System.currentTimeMillis());
        this.startTime = System.currentTimeMillis();
    }

    @AfterEach
    public void testAfterEach(){
        System.out.println("执行结束时间"+System.currentTimeMillis());
        this.endTime = System.currentTimeMillis();
        System.out.println("方法耗时："+(this.endTime-this.startTime));
    }

    @Test
    void testAsyncTask() {
        List<String> list = List.of("1", "2", "3", "4", "5", "6");
        List<CompletableFuture<String>> list1 = list.stream().map(item -> CompletableFuture.supplyAsync(() -> {
            log.info("item:{}  thread:{}", item, Thread.currentThread().getName());
            return item + Thread.currentThread().getName();
        }).thenApply(str -> { //同一个线程中处理
            log.info("str:{}  thread:{}", str, Thread.currentThread().getName());
            return str;
        }).thenCompose(str -> CompletableFuture.supplyAsync(() -> { //不同的线程中处理
            log.info("thenCompose value:{}   thread:{}",str, Thread.currentThread().getName());
            return str;
        }))).toList();
        CompletableFuture.allOf(list1.toArray(new CompletableFuture[0])).join();
        list1.forEach(item -> {
            log.warn("异步处理结果集：{}", item.join());
        });
    }


    @Test
    void testVoid(){
        log.info("对比结果：{}", Objects.equals(null,null));
    }

}
